package com.is.datatransform;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

@SuppressWarnings("serial")
public class ExportDo extends HttpServlet {

    @SuppressWarnings({ "unchecked", "deprecation" })
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
    	response.reset();//一定要加这一句话！！！！！
        response.setContentType("text/html");
	    HashMap<String, String> exportOps = (HashMap<String, String>)request.getSession().getAttribute("exportOps");
	    List exportList = (List)request.getSession().getAttribute("exportList");
		String className = (String)request.getSession().getAttribute("className");
	    String path = request.getRealPath("/");
		String fileName = System.currentTimeMillis() + ".xls";
		String realPath = path + "\\DownloadXls\\" + fileName;
		request.getSession().removeAttribute("realPath");
		request.getSession().setAttribute("realPath",realPath);
		try {
			Export.ExportExcel(realPath, exportList, exportOps, className);//生成excel
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		  SmartUpload smartUpload = new SmartUpload();
			//PageContext是jsp的内置对象，在servlet不能直接使用，需要做一些处理
//			  JspFactory _jspxFactory = null;
//		        PageContext pageContext = null;
//		        _jspxFactory = JspFactory.getDefaultFactory();
//		        pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);
//			   smartUpload.initialize(pageContext);
			 //或者下面一句代码
			   smartUpload.initialize(this.getServletConfig(),request,response);
			   smartUpload.setContentDisposition(null);
			    try {
					smartUpload.downloadFile(realPath);//下载excel
				} catch (SmartUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    }
  
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    	doPost(request, response);
    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
	// Put your code here
    }

}
